package com.fsx.servertokenlogin.controller;

import com.fsx.servertokenlogin.config.JwtConfg;
import com.fsx.servertokenlogin.dto.LoginResult;
import com.fsx.servertokenlogin.service.ILoginService;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.JwtException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class tokenCon {
    @Autowired
    JwtConfg jwtConfg;

    @Autowired
    ILoginService service;

    @Autowired
    StringRedisTemplate redisTemplate;

    @RequestMapping("/tokenlogin")
    public LoginResult<Boolean> tokenLogin(String token,String fingerprint){
        Claims claims =jwtConfg.checkToken(token,fingerprint);
        String uid=claims.getSubject();
        if(token.equals(redisTemplate.opsForValue().get(uid))){
            return new LoginResult<>(true);
        }else {
            throw new JwtException("已在其他客户端登录");
        }
    }
    @RequestMapping("/login")
    public LoginResult<String> login(String uid, String pwd,String fingerprint){
        return new LoginResult<>(service.login(uid, pwd, fingerprint));
    }
}
